import axios from 'axios';
import { message} from 'ant-design-vue';
import router from '../router/router';
import { checkUser, getLoginToken } from './authorization';

//数据请求字符
//axios.defaults.baseURL = 'http://127.0.0.1:10010/gatewayApi';
axios.defaults.baseURL = 'http://127.0.0.1:10015';
// axios.defaults.baseURL = process.env.VUE_APP_API_URL,
// 如果请求话费了超过 `timeout` 的时间，请求将被中断
axios.defaults.timeout = 5000;
// 表示跨域请求时是否需要使用凭证
axios.defaults.withCredentials = false;
// axios.defaults.headers.common['token'] =  AUTH_TOKEN
//axios.defaults.headers.post['Content-Type'] = 'application/json;charset=UTF-8';
// 允许跨域
axios.defaults.headers.post["Access-Control-Allow-Origin-Type"] = "*";

// 请求拦截器  window.location.href = window.location.origin + '/portal/';
export const $http = axios.create({});

//请求拦截器
const loginUrl = "/gatewayServer/userServeController/userLoginVerify";
const smsUrl = "/gatewayServer/userServeController/getSmsCodeByPhone"
const sendEmailUrl = "/gatewayServer/userServeController/getEmailCodeByEmail"
const picImg = "/easyExcelHandler/simpleUpload"
// let idempotenceKey = ['mis-preLoginKeySSH','mis-smsLoginSendCodeOperationKey','mis-emailLoginSendCodeOperationKey']
//预登录路径
const preLogin ="/gatewayServer/userServeController/preLoginCheck"
$http.interceptors.request.use(
    function(config){

        if(config.url.indexOf(picImg) !== -1){
            return config;
        }

        if(config.url.indexOf(preLogin) !== -1){
            config.headers['idempotenceKey'] = 'mis-preLoginKeySSH'
            return config;
        }
        if(config.url.indexOf(smsUrl) !== -1){
            config.headers['idempotenceKey'] = 'mis-smsLoginSendCodeOperationKey'
            return config;
        }
        if(config.url.indexOf(sendEmailUrl) !== -1){
            config.headers['idempotenceKey'] = 'mis-emailLoginSendCodeOperationKey'
            return config;
        }
        if(config.url.indexOf(loginUrl) !== -1){
            return config;
        }
        if(checkUser()){
            //config.headers['Content-Type'] = 'application/json';
            config.headers['Accept-Language'] = 'zh-CN';
            //config.headers['Authorization'] = currentUser.token;
            config.headers['token'] = getLoginToken();
        }else{
            // 请求路径
            message.warning("账号未登录,请登录!");
            //console.log("请求地址:==>"+config.baseURL+config.url);
            router.push("/");
        }
        return config;
    },
    function(error){
        message.error('请求超时,请重新登录!!!');
        return Promise.reject(error);
    }
);

// 响应拦截器
// axios.interceptors.response.use(
//     resp => {
//         return resp.data;
//     },
//     (err) => {
//         if(err.response.status === 401){
//             alert("登录已过期，请重新登录");
//             message.error('请求超时,请重新登录!!!');
//             let url = window.location.href;
//             url = url.replace("http://", "").replace("https://", "");
//             let baseUrl = url.split("/")[0];
//             window.location.href = "http://" + baseUrl + "/";
//         }
//         try {
//             return Promise.reject(err.response.data);
//         } catch (e) {
//             return Promise.reject(err);
//         }
//     }
// );